package com.xlhj.yucloud.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xlhj.yucloud.common.ResultData;
import com.xlhj.yucloud.entity.SysRoleInfo;
import com.xlhj.yucloud.entity.SysUserInfo;
import com.xlhj.yucloud.service.SysRoleInfoService;
import com.xlhj.yucloud.web.model.RoleInfoReqDto;
import com.xlhj.yucloud.web.model.RoleStatusReqDto;
import com.xlhj.yucloud.web.service.LoginService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: liucaijing
 * @description: SysRoleController
 * @date: 2021/9/27 15:02
 */
@RestController
@RequestMapping("/system/role")
@RequiredArgsConstructor
public class SysRoleController {

    private final SysRoleInfoService roleInfoService;
    private final LoginService loginService;

    /**
     * 分页条件查询角色信息
     * @param reqDto
     * @return
     */
    @GetMapping("/getRolePageList")
    public ResultData getRolePageList(RoleInfoReqDto reqDto) {
        Page<SysRoleInfo> roleInfoPage = roleInfoService.selectRoleInfoPageList(reqDto);
        return ResultData.success().data(roleInfoPage);
    }

    @GetMapping("/getRoleInfo/{roleId}")
    public ResultData getRoleInfo(@PathVariable(value = "roleId") Long roleId) {
        SysRoleInfo roleInfo = roleInfoService.getById(roleId);
        return ResultData.success().data(roleInfo);
    }

    @PostMapping("/addRoleInfo")
    public ResultData addRoleInfo(@RequestBody SysRoleInfo roleInfo) {
        Long nameCount = roleInfoService.checkRoleNameUnique(roleInfo.getRoleName());
        if (nameCount.longValue() > 0) {
            return ResultData.error().message("新增角色[" + roleInfo.getRoleName() + "]失败,角色名称已存在!");
        }
        Long keyCount = roleInfoService.checkRoleKeyUnique(roleInfo.getRoleKey());
        if (keyCount.longValue() > 0) {
            return ResultData.error().message("新增角色[" + roleInfo.getRoleName() + "]失败,角色权限已存在!");
        }
        boolean save = roleInfoService.save(roleInfo);
        if (save) {
            return ResultData.success();
        } else {
            return ResultData.error();
        }
    }

    @PutMapping("/changeRoleStatus")
    public ResultData changeRoleStatus(@RequestBody RoleStatusReqDto reqDto) {
        SysUserInfo userInfo = loginService.getUserInfo();
        roleInfoService.changeRoleStatus(reqDto.getRoleId(), reqDto.getStatus(), userInfo.getUserName());
        return ResultData.success();
    }

    @DeleteMapping("/delRoleInfo/{roleIds}")
    public ResultData delRoleInfo(@PathVariable("roleIds") Long[] roleIds) {
        roleInfoService.deleteRoleByIds(roleIds);
        return ResultData.success();
    }
}
